package tr.com.netgis;

import java.util.List;

import tr.com.netgis.db.NGDbHelper;
import tr.com.netgis.db.NGDbHelper.Table;
import tr.com.netgis.log.MessagesSqlCursorAdapter;
import tr.com.netgis.pojo.Message;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

/**
 * List View for deleting messages
 * @author NetGis
 *
 */
public class MessagesDeleteListActivity extends NGListActivity {

	private ListView mainListView = null;
	private Button btnDelete, btnCancel;
	private ProgressDialog dialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.messages_delete);

		btnDelete = (Button) findViewById(R.id.messagesDeleteBtnDelete);
		btnDelete.setEnabled(false);
		
		class DeleteTask extends AsyncTask<Void, Void, String> {

			protected String doInBackground(Void... vs) {
				List<Message> selectedMessages = ((MessagesSqlCursorAdapter)mAdapter).getSelectedMessages();
				int [] ids = new int[selectedMessages.size()]; 
				int i = 0;
				for (Message message : selectedMessages) {
					ids[i++] = message.get_id();
				}
				if (ids.length > 0) {
					mHelper.deleteMessage(ids);
				}
				return ids.length + " Mesaj silindi.";
			}

			protected void onProgressUpdate(Void... progress) {
			}

			protected void onPostExecute(String result) {
				Toast.makeText(MessagesDeleteListActivity.this, result, Toast.LENGTH_LONG).show();
				dismissProgress();
				MessagesDeleteListActivity.this.cancel();
			}
		};
		
		btnDelete.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				dialog = ProgressDialog.show(MessagesDeleteListActivity.this, "",
						"Mesajlar siliniyor...",
						true);
				new DeleteTask().execute(new Void[0]);
			}
		});
		
		btnCancel = (Button) findViewById(R.id.messagesDeleteBtnCancel);
		btnCancel.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				cancel();
			}
		});
		

		mainListView = (ListView) findViewById(R.id.messagesList);
		mainListView.setCacheColorHint(0);
		mainListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		
		mainListView.setOnItemLongClickListener(new OnItemLongClickListener() {
			
			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				return false;
			}
			
		});
		
		mainListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				
			}
			
		});

		// Bind the data with the list
		mainListView.setItemsCanFocus(false);
		mainListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

		//LoadSelections();
		new SelectDataTask().execute();
	}
	
	private void dismissProgress() {
		dialog.dismiss();
	}

	
	@Override
    protected void onRestart() {
        super.onRestart();
        new SelectDataTask().execute();
    }
	
	@Override
	protected void onPause() {
		super.onPause();
		mHelper.close();
	}
	
	private class SelectDataTask extends AsyncTask<String, Void, String> {

		protected void onPreExecute() {
		}

		protected String doInBackground(final String... args) {
			return null;
		}

		// can use UI thread here
		protected void onPostExecute(final String result) {
			String[] columns = NGDbHelper.getColumns(Table.MESSAGES);
	        mCursor = mDb.query(NGDbHelper.TABLE_NAME_MESSAGES, columns, null, null, null, null, null, null);
	        String[] headers = new String[] {NGDbHelper.COL_SENDER, NGDbHelper.COL_COMMAND};
	        
	        mAdapter = new MessagesSqlCursorAdapter(MessagesDeleteListActivity.this,
	        		R.layout.messages_delete_row,
	        		mCursor,
	        		headers, 
	                new int[]{android.R.id.text1, android.R.id.text2},
	        		mHelper);
	        mainListView.setAdapter(mAdapter);

		}
	}
	
	private void cancel(){
		this.finish();
	}
	

}
